home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part1 / 864 < prev    next >
Encoding:
Text File  |  1996-08-05  |  4.5 KB  |  100 lines

  1. Newsgroups: comp.lang.c
  2. Path: news.sprintlink.net!eskimo!scs
  3. From: scs@eskimo.com (Steve Summit)
  4. Subject: Re: start array at k, not 0
  5. X-Nntp-Posting-Host: eskimo.com
  6. Message-ID: <DKxFz0.B0q@eskimo.com>
  7. Keywords: Numerical Recipes in C
  8. Sender: news@eskimo.com (News User Id)
  9. Organization: schmorganization
  10. References: <30f06d21.167303296@nntp.ix.netcom.com> <tbutler.821142000@laraby.tiac.net> <DKwoyx.MD@news.cern.ch>
  11. Date: Tue, 9 Jan 1996 18:42:35 GMT
  12.  
  13. In article <DKwoyx.MD@news.cern.ch>, loreti@mxsld2.pd.infn.it writes:
  14. > In article <tbutler.821142000@laraby.tiac.net>, tbutler@laraby.tiac.net (Tim Butler) writes:
  15. >>miker3@ix.netcom.com (Mike Rubenstein) writes:
  16. >>: It's unfortunate that the authors of this book didn't bother to learn
  17. >>: C before writing it.  As anyone who has read the FAQ [6.13] knows,
  18. >>: this results in undefined behavior.  
  19. >>
  20. >> They have addressed that in the second edition. To retain the ability
  21. >> to start indexing at one, they "waste" that first element. They
  22. >> allocate one element extra.
  23. > Sorry, wrong.
  24.  
  25. Not quite.
  26.  
  27. > From the second edition, 1992 (ISBN 0-521-43108-5),
  28. > page 19 (emphasis on PERVERSELY mine):
  29. > "But suppose that your vector of length 7, now call it a ... is
  30. > PERVERSELY a native C, zero-offset array. ... Heaven help you! ... You
  31. > simply invoke someroutine(a-1,7) ... We want to free you from the
  32. > zero-offset thinking that C encourages ...
  33. > Of course, nrutil.c in Appendix B uses the same WRONG trick.  Please,
  34. > realize that Press, Teukolsky, Vetterling and Flannery MAY know
  35. > algorithms... but FOR SURE they do NOT know C.
  36.  
  37. Well, it's obvious that plenty of people by now have made them
  38. aware of the problem.  Tim Butler is right: the Numerical Recipes
  39. (second edition) code is correct for 1-based arrays, and Appendix
  40. B in that edition devotes several paragraphs to the topic:
  41.  
  42.     Strictly speaking, this scheme is not blessed by the ANSI
  43.     C standard.  The problem is *not* the fact that b-1
  44.     points to unallocated storage: location b-1 will never be
  45.     referenced without an increment back into the allocated
  46.     region.  Rather, the problem is that it might happen in
  47.     rare cases (and probably only on a segmented machine)
  48.     that the expression b-1 has *no representation at all*.
  49.     If this occurs, then there is no guarantee that the
  50.     relation b=(b-1)+1 is satisfied.
  51.  
  52.     In practice, this is much less of a problem than one
  53.     might think.  We are not aware of any compiler or machine
  54.     on which b=(b-n)+n fails to be true for small integer n...
  55.     The memory allocation routines in the First Edition of
  56.     Numerical Recipes in C, in wide use since 1988, all have
  57.     this "problem," and we have had not even a single report
  58.     of their failure in this respect (notwithstanding the
  59.     many readers who have told us that *theoretically* it
  60.     could fail)...
  61.  
  62.     Despite the absence of any experimental (as opposed to
  63.     theoretical) problem, we have taken some steps in this
  64.     edition to make our vector and matrix allocation routines
  65.     more ANSI compliant.  In the listing that follows, the
  66.     parameter NR_END is used as a number of extra storage
  67.     locations allocated *at the beginning* of every vector or
  68.     matrix block, simply for the purpose of making offset
  69.     pointer representations guaranteed-representable.  We set
  70.     NR_END to a default value of 1.  This has the effect of
  71.     making all *unit-offset* allocations... be strictly ANSI
  72.     compliant.  With NR_END = 1, the number of storage
  73.     locations wasted is fairly negligible.  Allocations with
  74.     offsets other than 1... are still theoretically
  75.     non-compliant, but are virtually unknown in our routines.
  76.  
  77. [Preceding text all from Numerical Recipes in C, Second Edition,
  78. 1992, ISBN 0-521-43108-5, Appendix B, p. 941; all emphasis theirs.]
  79.  
  80. So, despite their somewhat grudging tone and their dangerous
  81. suggestion that computing is properly an experimental science,
  82. I think they've made a pretty good compromise.  People who
  83. ridicule Numerical Recipes usually claim that they ought to
  84. "just" convert all the algorithms to be 0-based, but I tend to
  85. agree with the authors that the algorithms would then "acquire a
  86. baggage of additional arithmetic in array indices that is, at
  87. best, distracting" [p. 18].  Wasting an array element to make
  88. 1-based subscripting convenient is hardly an unknown or
  89. unthinkable practice: no less an authority than K&R explicitly
  90. recommends it (section 5.7), at least when space is not at a
  91. premium.  (To be sure, wasting an entire row and column, to
  92. achieve a 1-based 2-dimensional array, can be considerably more
  93. expensive.)
  94.  
  95.                     Steve Summit
  96.                     scs@eskimo.com
  97.